package com.lovi.utils;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * 手写两个输入输出流的工具类
 */
public class StreamUtils {
    /**
     * 把输入流拿到的数据转化成bytes数组
     */
    public static byte[] streamToBytes(InputStream inputStream) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        BufferedInputStream bis = new BufferedInputStream(inputStream);
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = bis.read(buffer))!= -1){
            baos.write(buffer,0,len);//把数据写到了baos中
        }
        byte[] byteArray = baos.toByteArray();
        baos.close();
        bis.close();
        return  byteArray;
    }

    /**
     * 把输入流拿到的数据转化成字符串
     */
    public static String streamToString(InputStream inputStream) throws IOException {
        return new String(streamToBytes(inputStream));
    }
}
